from urllib.parse import urlparse

from tornado.ioloop import IOLoop
from tornado.options import define,parse_command_line,options
from tornado.web import Application
from tornado.websocket  import WebSocketHandler

define('debug', default=False,type=bool,help='Run in debug mode')
define('port', default=8080,type=int,help='Server port')
define('allowed_hosts',default='localhost:8080', multiple=True, help='Allowed hosts for cross domain connections')

class SprintHandler(WebSocketHandler):
	def check_origin(self,origin):
		allowed = super().check_origin(origin)
		parsed = urlparse(origin.lower())
		matched = any(parsed.netloc == host for host in options.allowed_hosts)
		return options.debug or allowed or matched

	def open(self,sprint):
		pass

	def on_message(self,message):
		pass

	def on_close(self):
		pass

if __name__ == "__main__":
	parse_command_line()
	application = Application([(r'/(?P<sprint>[0-9]+)',SprintHandler),],debug=options.debug)
	application.listen(options.port)
	IOLoop.instance().start()

